package com.spirng.leetcode.day01;

public class ArrayTest6 {
    public static void main(String[] args) {
      int [][] matrix = new int[][]{{1,2,3},{8,9,4},{7,6,5}};
       ArrayTest6.generateMatrix(4);
    }
    public static   int[][] generateMatrix(int n) {
       int [][] result = new int[n][n];
       int startx=0;
       int starty=0;
       int i,j=0;
       int offset=1;
       int count=1;
       if (n%2==1){
           result[n/2][n/2]=n*n;
       }
       for (int quad=0;quad<n/2;quad++){
          for (j=starty;j<n-offset;j++){
              result[startx][j]=count++;
          }
          for (i=startx;i<n-offset;i++){
              result[i][j]=count++;
          }
          for (j=i;j>starty;j--){
              result[i][j]=count++;
          }
          for (;i>startx;i--){
              result[i][starty]=count++;
          }
          startx++;
          starty++;
          offset++;
       }
       return result;
    }
}
